<%@page import="util.Executor" %><%@page import="java.util.HashMap" %><%@page import="com.google.gson.*" %><%@page import="java.net.URLDecoder" %><%
    request.setCharacterEncoding("UTF-8");
	response.setCharacterEncoding("UTF-8");

	if (request.getParameter("className") != null) {
		String className = "managers." + request.getParameter("className");
		String methodName = request.getParameter("method");
		String prms = request.getParameter("params");

		System.out.println("params: " + prms);
		Object rst = Executor.exec(className, methodName, prms);
		out.print(new Gson().toJson(rst));
	}

    if (request.getParameter("execMethods") != null) {
        HashMap<String, Object> resp = new HashMap<String, Object>();

        String[] methods = request.getParameter("execMethods").split(",");
        int nm = methods.length, m;

        for (m = 0; m < nm; m ++) {
            String method = methods[m];
            HashMap<String, String> mm = Executor.getParams(method);
            String className = "managers." + mm.get("className");
            String methodName = mm.get("method");
            String prms = URLDecoder.decode(mm.get("params"), "UTF-8");

            //System.out.println("params: " + prms);
            Object rst = Executor.exec(className, methodName, prms);
            resp.put(methodName, rst);
        }
        
        out.print(new Gson().toJson(resp));
    }

    if (request.getParameter("getPopup") != null) {
        String idpp = request.getParameter("getPopup");
        String title = request.getParameter("title");
        String[] mtd = request.getParameter("method").split("[.]");
        String className = mtd[0];
        String method = mtd[1];
        String mParams = request.getParameter("mParams");
        String[] hFields = request.getParameter("heaFields").split(",");
        String fields = request.getParameter("fields");
        String putFunc = request.getParameter("putFunc");
        String putFields = request.getParameter("putFields");

        String params = mParams + (!mParams.equals("") ? "&" : "") + "String="+fields+"&String="+putFunc+"&String="+putFields;
        
        int nf = Integer.parseInt(request.getParameter("nf"));
%>
        <h2 onmousedown="setFloating(event);">
            <span><%= title %></span> 
            <a onclick="removeElement(this.parentNode.parentNode);" title="Cerrar" class="butt-close" href="JavaScript:;"></a>
        </h2>
        <article>
            <dialog>
                <div>
                    <label>
                        <span><%= hFields[nf] %>:</span>
                        <input onkeyup="searchInPopup(event, '<%= idpp %>', <%= nf %>);" />
                    </label>
                </div>
                <table id="tbl-<%= idpp %>" cellspacing="1" summary="<%= hFields.length %>,5">
                    <thead>
                        <tr>
                            <%
                                for (int h = 0; h < hFields.length; h ++) {
                            %>
                                    <th><%= hFields[h] %></th>
                            <%
                                }
                            %>
                        </tr>
                    </thead>
                    <tbody title="-1"><%= Executor.exec("managers." + className, method, params) %></tbody>
                </table>
            </dialog>
        </article>
<%

    }

    if (request.getParameter("report") != null) {
%>
        <%@page import="java.util.Map" %>
		<%@page import="java.io.*" %>
		<%@page import="java.net.URL" %>
		<%@page import="com.lowagie.text.DocumentException" %>
		<%@page import="org.xhtmlrenderer.pdf.ITextRenderer" %>
<%
        String report   = request.getParameter("report");
        String title    = java.net.URLEncoder.encode(request.getParameter("title"), "UTF-8");
		String f        = request.getParameter("file");
		String ori      = request.getParameter("orientacion");
		String html     = request.getParameter("html");

        String ops = "";
        Map<String, String[]> params = request.getParameterMap();
        //out.println(new Gson().toJson(params));

        for (Map.Entry<String, String[]> param : params.entrySet()) {
            String key = param.getKey();
            if (!key.equals("report") && !key.equals("title") && !key.equals("file") && !key.equals("orientacion") && !key.equals("html")) {
                ops += key + "=" + param.getValue()[0] + "&";
            }
        }

		if (!html.equals("")) {
%>
            <%@page import="util.Report" %>
<%
			Report.html = html;
		}

		String inputFile = request.getRequestURL().toString().replaceFirst("processor", "pdf") + "?report="+report+"&title="+title+"&"+ops+"orientacion="+ori;
		//out.print(inputFile);

		String file = getServletContext().getRealPath("/") + "docs\\" + f;

		//out.println(file);
		boolean res = true;
		try {

		    OutputStream os = new FileOutputStream(file);

		    ITextRenderer renderer = new ITextRenderer();
		    renderer.setDocument(inputFile);
		    renderer.layout();

		    renderer.createPDF(os);

		    os.close();
		} catch (FileNotFoundException e) {
		    e.printStackTrace();
		    res = false;
		} catch (DocumentException e) {
		    e.printStackTrace();
		    res = false;
		} catch (IOException e) {
		    e.printStackTrace();
		    res = false;
		}

		out.print(res);
	}

    if (request.getParameter("createExcel") != null) {
        String url = request.getRequestURL().toString().replace("processor.jsp", "") + request.getParameter("createExcel");
%>
        <%@page import="util.Excel" %>
        <% Excel.HTML2Excel(url, request.getParameter("file")); %>
<%
    }
%>
